import org.asynchttpclient.util.Assertions;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebElement;

import static java.lang.Thread.sleep;

public class HomePage extends Utils{

    public static String url = "http://127.0.0.1:8080/blog_system/blog_login.html";

    public HomePage() {
        super(url);
    }

    void PublishBlog() throws InterruptedException {
        // 打开博客列表页
        driver.get("http://127.0.0.1:8080/blog_system/blog_list.html");
        // 点击写博客按钮
        driver.findElement(By.cssSelector("body > div.nav > a:nth-child(5)")).click();
        sleep(3000);
        // document.querySelector("#title").value="测试"
        // 输入标题（通过Selenium执行JS脚本完成）
        ((JavascriptExecutor)driver).executeScript("document.querySelector(\"#title\").value=\"测试\"");
        sleep(2000);
        // 点击发布博客按钮
        driver.findElement(By.cssSelector("#submit")).click();
        sleep(3000);
        // 校验
        // 1、找到发布成功的博客标题
        WebElement title = driver.findElement(By.cssSelector("body > div.container > div.right > div > div.title"));
        sleep(2000);
        // 2、校验标题对应的元素是否为空，如果不为空，测试通过，如果为空，测试不通过
        if (title == null) {
            throw new RuntimeException();
        }
    }

    void DeleteBlog() throws InterruptedException {
        driver.get("http://120.78.126.148:8080/java100_blog/blog_list.html");
        sleep(3000);
        driver.findElement(By.cssSelector("body > div.container > div.right > div:nth-child(1) > a")).click();
        sleep(2000);
        driver.findElement(By.cssSelector("body > div.nav > a:nth-child(7)")).click();
        sleep(2000);
        String url = driver.getCurrentUrl();
        if (!url.equals("http://127.0.0.1:8080/blog_system/blog_list.html")) {
            throw new RuntimeException();
        }
    }
}
